<?php

namespace App\Http\Controllers\Api;

use Illuminate\Http\Request;
use EasyWeChat;

class WechatMiniController extends BaseController
{
    /*
     * 根据code获取openid和session_key
     */
    public function get_mini_session(Request $request)
    {
        $code = $request->input('code');

        if(empty($code))
        {
            return $this->error(10000,'code不能为空');
        }

        $miniProgram = EasyWeChat::miniProgram(); // 小程序
        $result = $miniProgram->auth->session($code);
        if(isset($result['errcode']))
        {
            return $this->error(10000,$result['errmsg']);
        }

        return $this->res([
            'data' => [
                'openid' => $result['openid'],
                'session_key' => $result['session_key'],
            ]
        ]);
    }
}
